/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package databases;

import Conexion.Conecxion;
import javax.swing.table.DefaultTableModel;
import Conexion.Conecxion;
import java.awt.event.KeyEvent;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Admin
 */
public class Cliente extends javax.swing.JFrame {
DefaultTableModel Modelo;
static Conecxion CP;
    /**
     * Creates new form Cliente
     */
    public Cliente(Conecxion CP){
        initComponents();
     
        this.CP=CP;
       Modelo=new DefaultTableModel();
       Modelo.addColumn("Codigo");
       Modelo.addColumn("Direccion");
       Modelo.addColumn("Saldo");
       Modelo.addColumn("Tope");
       Modelo.addColumn("Descuento");
       Modelo.addColumn("Descripcion");
       this.Clientes.setModel(Modelo);
       Actualizar();
    }
    
    
        
    private void Actualizar()
    {
         
        int CantidadFilas=Clientes.getRowCount();
        for(int i=CantidadFilas-1;i>=0;i--)
        {
            Modelo.removeRow(i);
        }
        
          try {
              
            ResultSet datos = this.CP.select("*", "\"schQuiz2\".cliente", "");
            String[]Arreglo=new String[6];
            
            while (datos.next()) {
//                this.TablaDeDatos.add(datos.getString(1)+" "+datos.getString(2) +" "+ datos.getString(3) + "\n");
                
                Arreglo[0]=datos.getString(1);
                Arreglo[1]=datos.getString(2);
                Arreglo[2]=datos.getString(3);
                Arreglo[3]=datos.getString(4);
                Arreglo[4]=datos.getString(5);
                Arreglo[5]=datos.getString(6);
                Modelo.addRow(Arreglo);
            }
            System.out.println("Actualizado");
        } catch (Exception e) {
            System.out.printf("Error: " + e.toString()+"\n");
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        Clientes = new javax.swing.JTable();
        Codigo = new javax.swing.JTextField();
        Saldo = new javax.swing.JTextField();
        Direccion = new javax.swing.JTextField();
        Descuento = new javax.swing.JTextField();
        Tope = new javax.swing.JTextField();
        Descripcion = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        Atras = new javax.swing.JButton();
        Insertar = new javax.swing.JButton();
        Actualizar = new javax.swing.JButton();
        Borrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Jordan");

        Clientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        Clientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ClientesMouseClicked(evt);
            }
        });
        Clientes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                ClientesKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(Clientes);

        jLabel1.setText("Codigo");

        jLabel2.setText("Saldo");

        jLabel3.setText("Direccion");

        jLabel4.setText("Tope");

        jLabel5.setText("Descuento");

        jLabel6.setText("Descripcion");

        Atras.setText("Atras");
        Atras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AtrasActionPerformed(evt);
            }
        });

        Insertar.setText("Insertar");
        Insertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InsertarActionPerformed(evt);
            }
        });

        Actualizar.setText("Actualizar");
        Actualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ActualizarActionPerformed(evt);
            }
        });

        Borrar.setText("Borrar");
        Borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BorrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(166, 166, 166)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Codigo)
                            .addComponent(Direccion)
                            .addComponent(Saldo, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Atras)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5))
                        .addGap(45, 45, 45)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(Descuento)
                            .addComponent(Descripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)))
                    .addComponent(jLabel4))
                .addGap(28, 28, 28))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 802, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(Insertar)
                        .addGap(157, 157, 157)
                        .addComponent(Actualizar)
                        .addGap(197, 197, 197)
                        .addComponent(Borrar)))
                .addContainerGap(39, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap(637, Short.MAX_VALUE)
                    .addComponent(Tope, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(28, 28, 28)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel4))
                    .addComponent(Atras))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Direccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Descuento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel3))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Saldo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel2))
                .addGap(33, 33, 33)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Insertar)
                    .addComponent(Actualizar)
                    .addComponent(Borrar))
                .addGap(25, 25, 25))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(25, 25, 25)
                    .addComponent(Tope, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(387, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AtrasActionPerformed
        // TODO add your handling code here:
        setVisible(false);
        Tabla T =new Tabla();
        T.setVisible(true);
    }//GEN-LAST:event_AtrasActionPerformed

    private void InsertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InsertarActionPerformed
        // TODO add your handling code here:
               try {
            boolean Insertar = this.CP.Insert("\"schQuiz2\".cliente", "'"+this.Codigo.getText()+"', '"+this.Direccion.getText()+"', "+this.Saldo.getText()+
                    ","+Tope.getText()+","+Descuento.getText()+",'"+Descripcion.getText()+"'");
           
              System.out.println("Dato Insertado");
            Actualizar();
        } catch (Exception e) {
            System.out.printf("Error: " + e.toString());
        }
        
    }//GEN-LAST:event_InsertarActionPerformed

    private void ClientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ClientesMouseClicked
        // TODO add your handling code here:
        
          try {
              
            ResultSet datos = this.CP.select("*", "\"schQuiz2\".cliente", "");
           
            
           
             int Columnas=Clientes.getSelectedRow();
             int Pasadas=0;
             
            while (datos.next()) {
//                this.TablaDeDatos.add(datos.getString(1)+" "+datos.getString(2) +" "+ datos.getString(3) + "\n");
              
                if(Pasadas==Columnas)
                {
               String A=datos.getString(1);
               String B= datos.getString(2);
               String C= datos.getString(3);
               String D= datos.getString(4);
               String E= datos.getString(5);
               String F= datos.getString(6);
            
                   this.Codigo.setText(A);
                   this.Direccion.setText(B);
                   this.Saldo.setText(C);
                   this.Tope.setText(D);
                   this.Descuento.setText(E);
                   this.Descripcion.setText(F);
                 
                   
                break;
                }
                
                Pasadas++;
            }
                  
            System.out.println("Datos Cargados");
        } catch (Exception e) {
            System.out.printf("Error: " + e.toString()+"\n");
        }
    }//GEN-LAST:event_ClientesMouseClicked

    private void ActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ActualizarActionPerformed
        // TODO add your handling code here:
        String Dato="";
                    try {
              
            ResultSet datos = this.CP.select("*", "\"schQuiz2\".cliente", "");
           
            
             int Pasadas=0;
             int Columnas=Clientes.getSelectedRow();
            while (datos.next()) {
//                this.TablaDeDatos.add(datos.getString(1)+" "+datos.getString(2) +" "+ datos.getString(3) + "\n");
              
                if(Pasadas==Columnas)
                {
                datos.getString(1);
                datos.getString(2);
                datos.getString(3);
                datos.getString(4);
                datos.getString(5);
                datos.getString(6);
                Dato=datos.getString(1);
                }
                
                Pasadas++;
            }
                
            System.out.println("Recorrido");
        } catch (Exception e) {
            System.out.printf("Error: " + e.toString()+"\n");
        }
       
     
            try {
            boolean 
             Actualiza = this.CP.Actualizar("\"schQuiz2\".cliente",
                     " codigo = " + this.Codigo.getText() + ", direccion= '"  + this.Direccion.getText() +
                             "', saldo= " + this.Saldo.getText() +", tope= "+this.Tope.getText()+", descuento= "+this.Descuento.getText()+", descripcion= '"+this.Descripcion.getText()+"'",
                     " codigo = '" + Dato +"'");  //Ojo Aqui
                   
           
              System.out.println("Dato Actualizado");
              Actualizar();
//              SetTexfield();
           
        } catch (Exception e) {
            System.out.printf("Error: " + e.toString()+"\n");
        }
        
    }//GEN-LAST:event_ActualizarActionPerformed

    private void BorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BorrarActionPerformed
        // TODO add your handling code here:
                 String Dato="";
            try {
              
            ResultSet datos = this.CP.select("*", "\"schQuiz2\".cliente", "");
           
            
             int Pasadas=0;
             int Columnas=Clientes.getSelectedRow();
            while (datos.next()) {
//                this.TablaDeDatos.add(datos.getString(1)+" "+datos.getString(2) +" "+ datos.getString(3) + "\n");
              
                if(Pasadas==Columnas)
                {
                datos.getString(1);
                datos.getString(2);
                datos.getString(3);
                datos.getString(4);
                datos.getString(5);
                datos.getString(6);
                Dato=datos.getString(1);
                }
                
                Pasadas++;
            }
                
            System.out.println("Vista De Datos");
        } catch (Exception e) {
            System.out.printf("Error: " + e.toString()+"\n");
        }
        
        
            try {
            boolean 
             Delete = this.CP.Delete("\"schQuiz2\".cliente","codigo = "+Dato);
                     
                   
           
              System.out.println("Dato Borrado");
            Actualizar();
//            SetTexfield();
        } catch (Exception e) {
            System.out.printf("Error: " + e.toString());
        }
        
    }//GEN-LAST:event_BorrarActionPerformed

           private String TirarDato()
    {
               String Copia ="";
            try {
              
            ResultSet datos = this.CP.select("*", "\"schQuiz2\".cliente", "");
           
            
           
             int Columnas=Clientes.getSelectedRow()-1;
             int Pasadas=0;
             
            while (datos.next()) {
//                this.TablaDeDatos.add(datos.getString(1)+" "+datos.getString(2) +" "+ datos.getString(3) + "\n");
              
                if(Pasadas==Columnas)
                {
                 Copia=datos.getString(1);
                }
                
                Pasadas++;
            }
                  
            System.out.println("Datos Cargados");
        } catch (Exception e) {
            System.out.printf("Error: " + e.toString()+"\n");
        }
            return Copia;
    }
    private void ClientesKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ClientesKeyTyped
        // TODO add your handling code here:
        
           char TeclaPresionada=evt.getKeyChar();
        if(TeclaPresionada==KeyEvent.VK_ENTER)
        {
                     try {
             
                         int Columnas=Clientes.getSelectedRow()-1;
                         
                         
                     boolean  Actualiza = this.CP.Actualizar("\"schQuiz2\".cliente",
                     " codigo = " + String.valueOf(this.Clientes.getValueAt(Columnas, 0).toString()) + ", direccion= '"  + String.valueOf(this.Clientes.getValueAt(Columnas, 1).toString()) +
                             "', saldo= " + String.valueOf(this.Clientes.getValueAt(Columnas, 2).toString()) +", tope= "+String.valueOf(this.Clientes.getValueAt(Columnas, 3).toString())
                             +", descuento= "+String.valueOf(this.Clientes.getValueAt(Columnas, 4).toString())+", descripcion= '"+String.valueOf(this.Clientes.getValueAt(Columnas, 5).toString())+"'",
                     " codigo = '" + TirarDato() +"'");  //Ojo Aqui
                   
           
              System.out.println("Dato Actualizado");
              Actualizar();
//              SetTexfield();
           
        } catch (Exception e) {
            System.out.printf("Error: " + e.toString()+"\n");
        }
        }
    }//GEN-LAST:event_ClientesKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Cliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Cliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Cliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Cliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                
               new Cliente(CP).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Actualizar;
    private javax.swing.JButton Atras;
    private javax.swing.JButton Borrar;
    private javax.swing.JTable Clientes;
    private javax.swing.JTextField Codigo;
    private javax.swing.JTextField Descripcion;
    private javax.swing.JTextField Descuento;
    private javax.swing.JTextField Direccion;
    private javax.swing.JButton Insertar;
    private javax.swing.JTextField Saldo;
    private javax.swing.JTextField Tope;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
